package distributed.lock;

import distributed.lock.redis.JedisClientRedisHandle;
import org.junit.Test;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;

import java.util.concurrent.*;
import java.util.concurrent.locks.ReentrantLock;

public class Demo1 {

    @Test
    public void demo2() {
        Config config = new Config();
        config.useClusterServers()
                .setScanInterval(2000) // cluster state scan interval in milliseconds
                .addNodeAddress("redis://127.0.0.1:7000", "redis://127.0.0.1:7001")
                .addNodeAddress("redis://127.0.0.1:7002");
        RedissonClient redisson = Redisson.create(config);
        RLock lock = redisson.getLock("anyLock");
        lock.lock();
        //lock.tr
    }

    @Test
    public void demo1() {
        ReentrantLock r;
        JedisClientRedisHandle jedisClientRedisHandle = new JedisClientRedisHandle();
        boolean a = jedisClientRedisHandle.setnx("a", "11111", 100, TimeUnit.SECONDS);
        System.out.println(a);
        boolean a1 = jedisClientRedisHandle.comparAndDel("a", "11111");
        System.out.println(a1);
    }


    @Test
    public void demo3() throws Exception {
        RedisAqsLock redisReentrantLock = new RedisAqsLock("L");
        redisReentrantLock.startGatekeeper();

        ExecutorService e = Executors.newScheduledThreadPool(30);
        CountDownLatch countDownLatch = new CountDownLatch(10);

        for (int i = 0; i < 10; i++) {
            e.submit(()->{
                try {
                    //System.out.println("获取锁 :"+Thread.currentThread().getName());
                    redisReentrantLock.tryLock(1000,TimeUnit.SECONDS);
                    System.out.println("拿到锁 :"+Thread.currentThread().getName());
                    Thread.sleep(1000);
                }catch (Exception ex){
                    ex.printStackTrace();
                }finally {
                    //System.out.println("释放锁 :"+Thread.currentThread().getName());
                    countDownLatch.countDown();
                    redisReentrantLock.unlock();
                }
            });
        }

        countDownLatch.await();


    }


    @Test
    public void demo4() throws Exception {
        RedisReentrantLock redisReentrantLock = new RedisReentrantLock();
        ExecutorService e = Executors.newScheduledThreadPool(30);
        CountDownLatch countDownLatch = new CountDownLatch(10);
        for (int i = 0; i < 10; i++) {
            e.submit(()->{
                try {
                    //System.out.println("获取锁 :"+Thread.currentThread().getName());
                    redisReentrantLock.tryLock("L",60,TimeUnit.SECONDS);
                    redisReentrantLock.tryLock("L",60,TimeUnit.SECONDS);
                    redisReentrantLock.unlock("L");
                    System.out.println("拿到锁 :"+Thread.currentThread().getName());
                    Thread.sleep(1000);
                }catch (Exception ex){
                    ex.printStackTrace();
                }finally {
                    //System.out.println("释放锁 :"+Thread.currentThread().getName());
                    countDownLatch.countDown();
                    redisReentrantLock.unlock("L");
                }
            });
        }

        countDownLatch.await();


    }



}
